package com.itfreer.data.dbregist;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.itfreer.data.db.dao.DBRegistDao;
import com.itfreer.data.db.entity.DBRegistEntity;
import com.itfreer.utils.hibernate.OrderConvertUtils;
import com.itfreer.utils.hibernate.PredicateConvertUtils;
import com.itfreer.utils.hibernate.QueryFieldsConvertUtils;

/**
 * 定义数据注册服务
 */
@Transactional(readOnly = false)
@Component("jdbcDBRegistDao")
public class JdbcDBRegistDao implements DBRegistDao {
	
	@Autowired
	protected SessionFactory sessionFactory;

	/**
	 * 获取实体列表
	 * 
	 * @param queryFields
	 *            查询字段
	 * @param where
	 *            查询条件
	 * @param order
	 *            排序条件
	 * @param pageSize
	 *            分页大小
	 * @param pageIndex
	 *            分页索引
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<DBRegistEntity> getEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order,
			Integer pageSize, Integer pageIndex) {

		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcDBRegistEntity.class);

		// 处理查询字段
		QueryFieldsConvertUtils.convert(crit, queryFields, JdbcDBRegistEntity.class);
		// 格式化查询条件
		PredicateConvertUtils.convert(crit, where);
		// 排序
		OrderConvertUtils.convert(crit, order);

		int startIndex = pageSize * (pageIndex - 1);
		crit.setFirstResult(startIndex);
		crit.setMaxResults(pageSize);
		List<JdbcDBRegistEntity> list = crit.list();

		List<DBRegistEntity> jEntityList = new ArrayList<DBRegistEntity>();

		if (list == null || list.size() <= 0) {
			return jEntityList;
		}
		for (JdbcDBRegistEntity entity : list) {
			if (entity == null) {
				continue;
			}
			jEntityList.add(entity.toEntity());
		}
		return jEntityList;
	}

	/**
	 * 获取当前条件下的数据行总数
	 * 
	 * @param where
	 *            查询条件
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Long getRowCounts(Map<String, Object> where) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcDBRegistEntity.class);
		// 格式化查询条件
		PredicateConvertUtils.convert(crit, where);
		crit.setProjection(Projections.rowCount());
		return (Long) crit.uniqueResult();
	}

	/**
	 * 获取实体
	 * 
	 * @param guid
	 *            唯一值
	 * @return
	 */
	@SuppressWarnings({ "deprecation" })
	public DBRegistEntity getEntity(String guid) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria crit = session.createCriteria(JdbcDBRegistEntity.class);
		crit.add(Restrictions.eq("id", guid));
		JdbcDBRegistEntity jEntity = (JdbcDBRegistEntity) crit.uniqueResult();
		if (jEntity == null) {
			return null;
		}
		return jEntity.toEntity();
	}

	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 */
	public DBRegistEntity add(DBRegistEntity entity) {
		if (entity == null) {
			return null;
		}
		Session session = this.sessionFactory.getCurrentSession();
		JdbcDBRegistEntity jEntity = new JdbcDBRegistEntity();
		jEntity.from(entity);
		session.save(jEntity);
		return jEntity.toEntity();
	}

	/**
	 * 更新实体
	 * 
	 * @param guid
	 *            唯一值
	 * @param entity
	 *            实体
	 * @return
	 */
	public DBRegistEntity update(DBRegistEntity entity) {
		if (entity == null) {
			return null;
		}
		Session session = this.sessionFactory.getCurrentSession();
		JdbcDBRegistEntity jEntity = new JdbcDBRegistEntity();
		jEntity.from(entity);
		session.update(jEntity);
		return jEntity.toEntity();
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public void delete(String guid) {
		Session session = this.sessionFactory.getCurrentSession();
		JdbcDBRegistEntity jEntity = new JdbcDBRegistEntity();
		jEntity.setId(guid);
		session.delete(jEntity);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	public void deletes(String guids) {
		if (guids == null || guids.trim().equals("")) {
			return;
		}
		Session session = this.sessionFactory.getCurrentSession();
		String[] ids = guids.split(",");
		for (String id : ids) {
			JdbcDBRegistEntity jEntity = new JdbcDBRegistEntity();
			jEntity.setId(id);
			session.delete(jEntity);
		}
	}
}